/*
 * @Author: your name
 * @Date: 2021-10-13 15:28:48
 * @LastEditTime: 2021-11-24 10:23:52
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \vue_demo\src\store\modules\permission.js
 */
import Layout from '@/layout'
const importer = require(`../../router/${process.env.NODE_ENV}`).default
const asyncRoutes = [

    {
        path: '/',
        component: Layout,
        redirect: '/myWait',
        id: 'M1102',
        children: [

            {
                path: '/myWait',
                name: 'myWait',
                isNeedConfirm: false,
                id: 'M1102',
                component: importer(/* webpackChunkName: "myWait" */ 'myWait/myWait'),
                meta: { title: '我的待办', icon: 'el-icon-s-order', affix: true, noCache: true }
            }

        ]
    },
    {
        path: '/mySponsor',
        component: Layout,
        id: 'M1112',
        children: [
            {
                path: '/mySponsor',
                name: 'mySponsor',
                isNeedConfirm: false,
                id: 'M1112',
                component: importer(/* webpackChunkName: "myWait" */ 'mySponsor/mySponsor'),
                meta: { title: '我的发起', icon: 'el-icon-s-flag', affix: false, noCache: true }
            }
        ]
    },

    // {
    //     path: '/overView',
    //     component: Layout,
    //     children: [
    //         {
    //             path: '/overView',
    //             name: 'overView',
    //             id: 'M1101',
    //             component: importer('overView/overView'),
    //             meta: { title: '看板', icon: 'el-icon-s-order', affix: false, noCache: true }
    //         }
    //     ]
    // },
    {
        path: '/feedBack',
        component: Layout,
        id: 'M1103',
        children: [
            { path: '/feedBack',
                name: 'feedBack',
                id: 'M1103',
                component: importer('feedBack/feedBack'),
                meta: { title: '反馈总览', icon: 'el-icon-s-promotion', affix: false, noCache: true }
            }
        ]
    },
    {
        path: '/linkDetail',
        component: Layout,
        hidden: true,
        id: '434',
        children: [
            {
                path: '/linkDetail',
                name: 'linkDetail',
                id: '434',
                component: importer(/* webpackChunkName: "Overview" */ 'feedBack/linkDetail'),
                meta: { title: '反馈详情', icon: 'el-icon-s-order', affix: false, noCache: true }
            }
        ]
    },
    {
        path: '/Details',
        component: Layout,
        hidden: true,
        id: 'M1108',
        children: [
            {
                name: 'Details',
                path: '/Details',
                id: 'M1108',
                component: importer('feedBack/details'),
                meta: { title: '反馈详情', icon: 'Details', affix: false, noCache: true }
            }
        ]
    },
    {
        path: '/task',
        id: 'M1104',
        component: Layout,
        hidden: false,
        children: [
            {
                name: 'task',
                path: '/task',
                id: 'M1104',
                component: importer(/* webpackChunkName: "task" */ 'task/task'),
                meta: { title: '任务总览', icon: 'el-icon-s-management', affix: false, noCache: true }
            }
        ]
    },
    {
        path: '/systemSetting',
        name: 'systemSetting',
        redirect: '/systemSetting/column',
        component: Layout,
        hidden: false,
        id: 'M1106',
        meta: { title: '系统设置', icon: 'el-icon-s-tools', affix: false, noCache: true },
        children: [
            {
                path: '/systemSetting/column',
                name: 'column',
                hidden: false,
                id: 'M1106',
                component: importer(/* webpackChunkName: "column" */ 'systemSetting/column'),
                meta: { title: '流程节点设置', icon: '', affix: false, noCache: true }
            },
            {
                path: '/systemSetting/marketingProStatus',
                name: 'marketingProStatus',
                hidden: false,
                id: 'M1106',
                component: importer(/* webpackChunkName: "marketingProStatus" */ 'systemSetting/marketingProStatus'),
                meta: { title: '自定义设置', icon: 'dashboard', affix: false, noCache: true }
            },
            // {
            //     path: '/systemSetting/messageSettings',
            //     name: 'messageSettings',
            //     hidden: false,
            //     id: 'M1106',
            //     component: importer(/* webpackChunkName: "messageSettings" */ 'systemSetting/messageSettings'),
            //     meta: { title: '消息设置', icon: 'dashboard', affix: false, noCache: true }
            // },
            {
                path: '/systemSetting/productSettings',
                name: 'productSettings',
                hidden: false,
                id: 'M1106',
                component: importer(/* webpackChunkName: "productSettings" */ 'systemSetting/productSettings'),
                meta: { title: '产品权限设置', icon: 'dashboard', affix: false, noCache: true }
            },
            {
                path: '/systemSetting/emileSettings',
                name: 'emileSettings',
                id: 'M1106',
                hidden: false,
                component: importer(/* webpackChunkName: "stageSettings" */ 'systemSetting/emileSettings'),
                meta: { title: '邮件设置', icon: 'dashboard', affix: false, noCache: true }
            },
            {
                path: '/systemSetting/overTineMeeting',
                name: 'overTineMeeting',
                id: 'M1106',
                hidden: false,
                component: importer(/* webpackChunkName: "stageSettings" */ 'systemSetting/overTineMeeting'),
                meta: { title: '超期设置', icon: 'dashboard', affix: false, noCache: true }
            },
            {
                path: '/systemSetting/noticeSetting',
                name: 'noticeSetting',
                id: 'M1106',
                hidden: false,
                component: importer(/* webpackChunkName: "stageSettings" */ 'systemSetting/noticeSetting/noticeSetting'),
                meta: { title: '公告设置', icon: 'dashboard', affix: false, noCache: true }
            }
        ]
    },

    {
        path: '/dataStatistics',
        component: Layout,
        hidden: false,
        id: 'M1105',
        children: [
            {
                name: 'dataStatistics',
                path: '/dataStatistics',
                id: 'M1105',
                component: importer(/* webpackChunkName: "task" */ 'dataStatistics/dataStatistics'),
                meta: { title: '数据统计', icon: 'el-icon-s-data', affix: false, noCache: false }
            }
        ]
    },
    {
        path: '/dataStaticDetail',
        component: Layout,
        hidden: true,
        id: 'M1110',
        children: [
            {
                id: 'M1110',
                path: '/dataStaticDetail',
                component: importer(/* webpackChunkName: "dataStaticDetail" */ 'dataStatistics/dataStaticDetail'),
                name: 'dataStaticDetail',
                meta: { title: '数据统计详情', icon: 'dashboard', affix: false, noCache: true }
            }
        ]
    },
    {
        path: '/delateMeeting',
        hidden: false,
        id: 'M1107',
        component: Layout,
        children: [
            {
                id: 'M1107',
                path: '/delateMeeting',
                name: 'delateMeeting',
                component: importer(/* webpackChunkName: "myWait" */ 'delateMeeting/delateMeeting'),
                meta: { title: '超期会议', icon: 'el-icon-s-help', affix: false, noCache: true }
            }
        ]
    },
    {
        path: '/mettingDetail',
        hidden: true,
        id: 'M1111',
        component: Layout,
        children: [
            {
                name: 'mettingDetail',
                path: '/mettingDetail',
                id: 'M1111',
                component: importer(/* webpackChunkName: "myWait" */ 'delateMeeting/mettingDetail'),
                meta: { title: '超期会议-详情', icon: 'dashboard', affix: false, noCache: true }
            }
        ]

    },

    {
        path: '/createFeedBack',
        hidden: true,
        component: Layout,
        children: [
            {
                name: 'createFeedBack',
                path: '/createFeedBack',
                id: '',
                component: importer(/* webpackChunkName: "createFeedBack" */ 'feedBack/createFeedBack/createFeedBack'),
                meta: { title: '创建反馈', icon: 'dashboard', affix: false, noCache: false }
            }
        ]

    },
    {
        path: '/editFeedBack',
        hidden: true,
        component: Layout,
        children: [
            {
                path: '/editFeedBack',
                name: 'editFeedBack',
                id: '',
                component: importer(/* webpackChunkName: "editFeedBack" */ 'feedBack/editFeedBack/editFeedBack'),
                meta: { title: '编辑反馈', icon: 'dashboard', affix: false, noCache: false }
            }
        ]

    },
    {
        path: '/taskDetail',
        hidden: true,
        id: 'M1109',
        component: Layout,
        children: [
            {
                path: '/taskDetail',
                name: 'taskDetail',
                id: 'M1109',
                component: importer(/* webpackChunkName: "taskDetail" */ 'task/taskDetails/taskDetails'),
                meta: { title: '任务详情', icon: 'dashboard', affix: false, noCache: true }
            }
        ]

    },
    {
        path: '/turnBug',
        hidden: true,
        component: Layout,
        children: [
            {
                path: '/turnBug',
                name: 'turnBug',
                id: '',
                component: importer(/* webpackChunkName: "turnBug" */ 'feedBack/turnBug/turnBug'),
                meta: { title: '转BUG', icon: 'dashboard', affix: false, noCache: false }
            }
        ]
    },
    {
        path: '/turnDemand',
        hidden: true,
        component: Layout,
        children: [
            {
                path: '/turnDemand',
                name: 'turnDemand',
                id: '',
                component: importer(/* webpackChunkName: "turnDemand" */ 'feedBack/turnDemand/turnDemand'),
                meta: { title: '转需求', icon: 'dashboard', affix: false, noCache: false }
            }
        ]

    },

    {
        path: '/login',
        name: 'Login',
        id: '',
        hidden: true,
        component: importer(/* webpackChunkName: "Login" */ 'login')
    }

]
const constantRoutes = [
    {
        path: '/redirect',
        name: 'redirect',
        component: Layout,
        id: '',
        hidden: true, isNeedConfirm: false,
        children: [
            {
                path: '/redirect/:path(.*)',
                meta: { title: 'redirect', icon: 'el-icon-s-order' },
                hidden: true, isNeedConfirm: false,
                component: importer('redirect/index')
            }
        ]
    },
    {
        path: '/404',
        hidden: true, isNeedConfirm: false,
        name: '404',
        id: '',
        component: importer(/* webpackChunkName: "myWait" */ 'constantPage/404'),
        meta: { title: '404', icon: 'el-icon-s-order', affix: false, noCache: true }
    }
]

/**
 * Use meta.role to determine if the current user has permission
 * @param roles
 * @param route
 */
// function hasPermission(roles, route) {
//     if (route.meta && route.meta.roles) {
//         return roles.some(role => route.meta.roles.includes(role))
//     } else {
//         return true
//     }
// }
export function dataSort(a, b) {
    return a['nums'] - b['nums']
}
/**
 * Filter asynchronous routing tables by recursion
 * @param routes asyncRoutes
 * @param roles
 */
export function filterAsyncRoutes(routes) {
    const res = []
    const sessionList = JSON.parse(sessionStorage.getItem('USERINFO'))
    if (sessionList && sessionList.permissonTree) {
        const treeData = sessionList.permissonTree
        treeData.forEach(items => {
            for (let i = 0; i < routes.length; i++) {
                const element2 = routes[i]
                if (element2.path === '/') {
                    element2.path = '/myWait'
                }
                if (element2.id == items.permCode) {
                    element2.buttonList = items.buttonList
                    res.push(element2)
                }
            }
        })
    }
    return res
}
/**
 * Filter asynchronous routing tables by recursion
 * @param routes asyncRoutes
 * @param roles
 */
export function filterAsyncButtonList(list, path) {
    const res = []

    if (list && list.length > 0) {
        list.forEach(ele => {
            if (ele.id && ele.id != '' && path == ele.path) {
                if (ele.buttonList) {
                    res.buttonList = ele.buttonList
                } else {
                    res.buttonList = []
                }

                if (ele.children) {
                    filterAsyncButtonList(ele.children, path)
                }
            }
            // res.push(tmp)
        })
    }
    return res
}

const state = {
    routes: [],
    addRoutes: [],
    buttonList: []
}

const mutations = {
    SET_ROUTES: (state, routes) => {
        state.addRoutes = routes
        state.routes = constantRoutes.concat(routes)
    },
    SET_BUTTON_LIST: (state, buttonList) => {
        state.buttonList = buttonList
    }
}

const actions = {
    generateRoutes({ commit }) {
        return new Promise(resolve => {
            const accessedRoutes = filterAsyncRoutes(asyncRoutes)

            accessedRoutes.forEach(element => {
                const nums = element.id.substr(element.id.length - 1, 1)
                element.nums = nums * 1
                if (element.id == 'M1101') {
                    element.nums = 2
                }
                if (element.id == 'M1102') {
                    element.nums = 1
                }
            })
            accessedRoutes.sort(dataSort)
            commit('SET_ROUTES', accessedRoutes)
            resolve(accessedRoutes)
        })
    },
    addButtonList({ commit }, to) {
        const list = filterAsyncButtonList(state.routes[0].children, to.path)
        return new Promise(resolve => {
            commit('SET_BUTTON_LIST', list)
            resolve(list)
        })
    }
}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}
